package com.ostrich.system.analysis;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.util.SpringContextUtil;
import com.ostrich.common.OstrichConstants;
import com.ostrich.manpower.common.FormulaUtil;
import com.ostrich.system.bean.TostSysParaminfo;
import com.ostrich.system.service.impl.TostSysParaminfoService;

@Component
public class ParaminfoUtil {
	@Autowired
	private static SpringContextUtil springContextUtil;
	@Autowired
	private static TostSysParaminfoService pmservice;

	// 缓存的公式信息
	private static Map<String, TostSysParaminfo> pminfo;
	
	// 缓存的公式信息
	private static Map<Integer, TostSysParaminfo> pminfoById;

	private static Map<Integer, Map<Integer, TostSysParaminfo>> pminfokeyPid;

	@PostConstruct
	public void init() {
		setPm();
	}

	/****
	 * 设置缓存内的公式信息
	 */
	public static void setPm() {
		if (pminfo == null) {
			pminfo = new LinkedHashMap<String, TostSysParaminfo>();
		}
		if(pminfoById==null){
			pminfoById = new LinkedHashMap<Integer, TostSysParaminfo>();
		}
		// 获取注入的service的bean信息
		if (pmservice == null)
			pmservice = (TostSysParaminfoService) springContextUtil
					.getBean("tostSysParaminfoService");
		List<TostSysParaminfo> ls = pmservice.queryByAll();
		if (ls != null)
			for (TostSysParaminfo pm : ls) {
				pminfo.put("#" + pm.getPid() + "#" + pm.getId(), pm);
				pminfoById.put(pm.getId(), pm);
			}
	}

	public static TostSysParaminfo getPminfoById(Integer id){
		if(pminfoById==null)
			setPm();
		return pminfoById.get(id);
	}
	
	public static void refresh() {
		pminfo.clear();
		setPm();
	}

	public static void refreshByPid(Integer pid) {
		pminfo.clear();
		setPm();
		if (pminfokeyPid == null) {
			pminfokeyPid = new LinkedHashMap<Integer, Map<Integer, TostSysParaminfo>>();
		}
		setParamByPid(pid);
	}

	/**
	 * 设置父节点相同的内容
	 * 
	 * @param pid
	 */
	public static void setParamByPid(Integer pid) {
		if (pid == null || pid < 0)
			return; // 如果为空就直接返回

		Map<Integer, TostSysParaminfo> map = new LinkedHashMap<Integer, TostSysParaminfo>();
		for (Map.Entry<String, TostSysParaminfo> entry : pminfo.entrySet()) {
			TostSysParaminfo tp = entry.getValue();
			if ((entry.getKey().toLowerCase()).startsWith("#" + pid + "#")) {
				map.put(tp.getId(), tp);
			}
		}
		pminfokeyPid.put(pid, map);
	}

	/****
	 * 
	 * @param pid
	 * @return
	 */
	public static Map<Integer, TostSysParaminfo> getParamByPid(Integer pid) {
		if (pid == null || pid <= 0)
			return null;

		if (pminfo != null) {
			if (pminfokeyPid == null) {
				pminfokeyPid = new LinkedHashMap<Integer, Map<Integer, TostSysParaminfo>>();
			}

			if (pminfokeyPid.get(pid) == null) {
				//由于岗位修改为岗位表的查询，所以从参数表作了相关转换
				if(pid==OstrichConstants.PARMHR_BASE_POSTNAME)
					setFormulaByPostinfo();
				//setParamByPid(pid);
				else{
					// 判断是否已经有该关键code的信息，如果没有就进行添加
					setParamByPid(pid);
				}
			}
		}
		return pminfokeyPid.get(pid);
	}
	/***
	 * 通过算法表来查询出岗位表信息，用来转化成参数信息表查询内容
	 * @return
	 */
	private static void setFormulaByPostinfo(){
		Map<String,Object> pfm = FormulaUtil.formulaById(OstrichConstants.PARMHR_BASE_POST_FORMULATABLE);
		List<Map<String,Object>> ls = (List<Map<String, Object>>) pfm.get("data");
		Map<Integer,TostSysParaminfo> smap = new HashMap<Integer,TostSysParaminfo>();
		for(Map<String,Object> mp:ls){
			TostSysParaminfo tsp = new TostSysParaminfo();
			tsp.setId((Integer) mp.get("id"));
			tsp.setPaName((String) mp.get("hpoName"));
			Float ft = (mp.get("hpoScale")==null)?new Float(0):(Float)(mp.get("hpoScale"));
			tsp.setPaValue(ft.toString());//岗位系数内容
			smap.put(tsp.getId(), tsp);
		}
		pminfokeyPid.put(OstrichConstants.PARMHR_BASE_POSTNAME, smap);
	}
	
	
	public static Map<String, TostSysParaminfo> getParamKeyNameByPid(Integer pid) {
		 Map<Integer, TostSysParaminfo> map = getParamByPid(pid);
		 Map<String, TostSysParaminfo> namemap = new HashMap<String,TostSysParaminfo>();
		 if(map!=null){
			 for (Map.Entry<Integer, TostSysParaminfo> entry : map.entrySet()) {
				 TostSysParaminfo tp =  entry.getValue();
				 namemap.put(tp.getPaName(), tp);
			 }
		 }
		return namemap;
	}
	
}
